package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Classinfo;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.ClassinfoQuery;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClassinfoService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("list")
    public String list(Model model, ClassinfoQuery classinfoQuery) {
        PageInfo classinfolist = classinfoService.list(classinfoQuery);
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("result", classinfolist);
        model.addAttribute("employees", employees);
        model.addAttribute("qo", classinfoQuery);
        return "classinfo/list";
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        classinfoService.delete(id);
        return new JsonResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo) {
        if (classinfo.getId() == null) {
            classinfoService.save(classinfo);
        } else {
            classinfoService.update(classinfo);
        }
        return new JsonResult();
    }
}
